<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Center extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->my_layout->setLayout("layout/frontend");
        $this->load->library('pagination');
        $this->load->model("center_model");
    }

    function index() {
        $sort = $this->input->post('sort');
        $orderBy = $this->input->post('orderBy');
        $data['sort'] = $sort;
        $data['orderBy'] = $orderBy;
        $data['path'] = base_url() . 'index.php/website/center';
        if (!$sort) {
            $sort = 'name';
        }
        if (!$orderBy) {
            $orderBy = 'asc';
        }
        if ($sort != 'name') {
            if ($orderBy == 'asc') {
                $orderBy = 'desc';
            } else {
                $orderBy = 'asc';
            }
        }
        $records = $this->center_model->get_list(null, $sort, $orderBy, null, null);

        $config['base_url'] = base_url() . 'index.php/website/center/index';
        $config['total_rows'] = count($records);
        $config['per_page'] = 10; //records
        $config['uri_segment'] = 4;
        $config['num_links'] = 10;
        $config['next_link'] = 'Next »';
        $config['prev_link'] = '« Prev';
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $this->pagination->initialize($config);

        $records = $this->center_model->get_list(null, $sort, $orderBy, $config['per_page'], $this->uri->segment(4));
        $data['listCenters'] = $records;
        $data['link'] = $this->pagination->create_links();



        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = null;
        $this->my_layout->view('frontend/center/list_center_view', $data);
    }

    function type($type) {
        $sort = $this->input->post('sort');
        $orderBy = $this->input->post('orderBy');
        $data['sort'] = $sort;
        $data['orderBy'] = $orderBy;
        $data['path'] = base_url() . 'index.php/website/center/type/' . $type;
        if (!$sort) {
            $sort = 'name';
        }
        if (!$orderBy) {
            $orderBy = 'asc';
        }
        if ($sort != 'name') {
            if ($orderBy == 'asc') {
                $orderBy = 'desc';
            } else {
                $orderBy = 'asc';
            }
        }

        $records = $this->center_model->get_list($type, $sort, $orderBy, null, null);

        $config['base_url'] = base_url() . 'index.php/website/center/type/' . $type;
        $config['total_rows'] = count($records);
        $config['per_page'] = 10; //records
        $config['uri_segment'] = 5;
        $config['num_links'] = 10;
        $config['next_link'] = 'Next »';
        $config['prev_link'] = '« Prev';
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $this->pagination->initialize($config);

        $data['listCenters'] = $this->center_model->get_list($type, $sort, $orderBy, $config['per_page'], $this->uri->segment(5));
        $data['link'] = $this->pagination->create_links();

        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = $type;
        $this->my_layout->view('frontend/center/list_center_view', $data);
    }

    function loadById($id) {
        $temp = $this->center_model->getById($id);
        if($temp != NULL){
            $this->center_model->increate_view($temp['id_center'], $temp['view']);
        }
        $data['images'] = $this->center_model->getListImages($id);
        $data['center'] = $temp;
        $this->load->library('googlemaps');
        /* config maps */
        $config['center'] = $temp['lat'] . ',' . $temp['long'];
        
        $config['map_width'] = '380px';
        $config['map_height'] = '200px';

        $this->googlemaps->initialize($config);
        /* create maker */
        $marker = array();
        $marker['position'] = $temp['lat'] . ',' . $temp['long'];
        $marker['infowindow_content'] = "<b>Tên trung tâm:" . $temp['name'] . "</br>" . "Địa chỉ: Đường "
                . $temp['street'] . ",Phường: " . $temp['ward'] . ", Quận: " . $temp['district'];
        $marker['icon'] = base_url("public/images/googlemaps/marker.png");
        /* create maps */
        $this->googlemaps->add_marker($marker);
        $data['map'] = $this->googlemaps->create_map();
        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = NULL;
        $this->my_layout->view('frontend/center/center_view', $data);
    }

}
